package com.hm.PushCaseService.service.push.qycg.push;

import com.hm.PushCaseService.constant.ReqUrl;
import com.hm.PushCaseService.pojo.CaseBean;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class PushCaseService implements IPushCase {
    private static final Logger logger = LoggerFactory.getLogger(PushCaseService.class);

    @Qualifier("OkHttpClient")
    public OkHttpClient okHttpClient;

    public PushCaseService(OkHttpClient okHttpClient) {
        this.okHttpClient = okHttpClient;
    }

    @Override
    public void push(CaseBean caseBean, String cookie, PushCaseListen listen) {
        //原始案件号
        String originalEventCode = caseBean.eventcode;
        //数据预处理
        HandlePushCaseUtils.handle(caseBean);
        //构建请求体
        MultipartBody body = new MultipartBody.Builder()//
                .addFormDataPart("eventCode", caseBean.eventcode)//案件编号
                .addFormDataPart("eventDesc", caseBean.eventDesc)//案件描述
                .addFormDataPart("status", caseBean.status)//案件状态字典
                .addFormDataPart("eventClass", caseBean.eventClass)//案件分类字典（默认值010901）
                .addFormDataPart("eventTime", caseBean.eventTime)//案件发生时间 yyyy-MM-dd HH:mm:ss
                .addFormDataPart("locationDesc", caseBean.locationDesc)//案件发生地址
                .addFormDataPart("currentStepName", caseBean.currentStepName)//当前环节名称(中文名称)
                .addFormDataPart("currentDeptName", caseBean.currentDeptName)//当前环节处理部门(中文名称)
                .addFormDataPart("currentDealPeopleName", caseBean.currentDealPeopleName)//当前环节处理人(中文名称)
                .addFormDataPart("fileUrls", caseBean.fileUrls)//图片链接（多个链接以英文逗号分隔）
                .build();

        //创建post请求
        Request request = new Request.Builder()//
                .addHeader("Cookie", cookie)//添加cookie
                .addHeader("Content-Type", "multipart/form-data")//添加请求类型
                .url(ReqUrl.AddOrUpdateUrl)//请求连接
                .post(body)//请求体
                .build();//构建

        try (Response response = okHttpClient.newCall(request).execute()) {
            //同步请求
            ResponseBody resBody = response.body();
            if (resBody == null) {
                String errMsg = "onResponse为null！！ ";
                listen.pushFailure(originalEventCode, errMsg);
                logger.error(errMsg);
                return;
            }
            //800200：推送成功
            String string = resBody.string();
            if (string.contains("800200")) {
                listen.pushSucceed(originalEventCode);
            } else {
                listen.pushFailure(originalEventCode, string);
            }
            logger.warn("案件：" + originalEventCode + "--->onResponse: " + string);
        } catch (Exception e) {
            String message = e.getMessage();
            listen.pushFailure(originalEventCode, message);
            logger.error("推送异常：Exception: " + message);
        }
    }

}
